import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

import '../../ylm_ui_kit.dart';

/// 提示框
class Loading {
  // 显示loading
  static Future show({
    String? text,
    VoidCallback? onDismiss,
    VoidCallback? onMask,
    bool clickMaskDismiss = true,
  }) async {
    await SmartDialog.showLoading(
      msg: text ?? '',
      clickMaskDismiss: clickMaskDismiss,
      onDismiss: onDismiss,
      onMask: onMask,
      maskColor: Colors.transparent,
      builder: (context) {
        return Container(
          alignment: Alignment.center,
          padding: EdgeInsets.all(20.w),
          width: 80.w,
          height: 80.w,
          decoration: CommonDecoration.getRadius(bg: Colors.black87, all: 10.w),
          child: CircularProgressIndicator(color: YlmGlobalConfig().loadingConfig.color),
        );
      },
    );
  }

  // 关闭loading
  static Future<void> dismiss() async {
    SmartDialog.dismiss(status: SmartStatus.loading);
  }

  // toast
  static Future toast(String text, {bool closeLoading = false}) async {
    if (closeLoading) await Loading.dismiss();
    await SmartDialog.showToast(
      text,
      alignment: Alignment.center,
      displayTime: const Duration(seconds: 1),
      builder: (_) {
        return Container(
          margin: EdgeInsets.symmetric(horizontal: 30.w),
          decoration: CommonDecoration.getShapeDecoration(radius: 6.w, bg: Colors.black.withOpacity(0.5)),
          padding: EdgeInsets.symmetric(horizontal: 30.w, vertical: 8.w),
          child: Text(
            text,
            style: const TextStyle(color: Colors.white, fontSize: 14),
            maxLines: 10,
            overflow: TextOverflow.ellipsis,
          ),
        );
      },
    );
  }

  ///返回[predicate]结果，如果true，则toast [msg]
  static bool predicate(bool Function() predicate, String msg, {bool closeLoading = false}) {
    bool result = predicate.call();
    if (result) toast(msg, closeLoading: closeLoading);
    return result;
  }
}
